package com.kgc.crowd.distributioncrowddatabaseprovider.service.impl;

import com.kgc.crow.distributioncrowdcommon.pojo.*;
import com.kgc.crow.distributioncrowdcommon.util.CrowdUtils;
import com.kgc.crow.distributioncrowdcommon.vo.MemberConfirmInfoVO;
import com.kgc.crow.distributioncrowdcommon.vo.MemberLauchInfoVO;
import com.kgc.crow.distributioncrowdcommon.vo.ProjectVO;
import com.kgc.crow.distributioncrowdcommon.vo.ReturnVO;
import com.kgc.crowd.distributioncrowddatabaseprovider.mapper.*;
import com.kgc.crowd.distributioncrowddatabaseprovider.service.ProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Transactional(readOnly = true)
@Service
public class ProjectServiceImpl implements ProjectService {
    @Resource
    ProjectPOMapper projectPOMapper;

    @Resource
    TypePOMapper typePOMapper;

    @Resource
    TagPOMapper tagPOMapper;

    @Resource
    ProjectItemPicPOMapper projectItemPicPOMapper;

    @Resource
    MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;

    @Resource
    ReturnPOMapper returnPOMapper;

    @Resource
    MemberConfirmInfoPOMapper memberConfirmInfoPOMapper;

    @Transactional(readOnly=false, propagation= Propagation.REQUIRES_NEW, rollbackFor=Exception.class)
    @Override
    public void saveProject(ProjectVO projectVO, String memberId){
        // 1.保存ProjectPO
        ProjectPO projectPO = new ProjectPO();
        BeanUtils.copyProperties(projectVO, projectPO);
        //把用户id写入projectPO
        int id = Integer.parseInt(memberId);
        projectPO.setMemberid(id);
        projectPOMapper.insert(projectPO);
        // 2.获取保存ProjectPO后得到的自增主键
        // 在ProjectPOMapper.xml文件中insert方法对应的标签中设置useGeneratedKeys="true" keyProperty="id"
        Integer projectId = projectPO.getId();
        System.out.println(projectId);

        //3.保存typeIdList
        List<Integer> typeIdList = projectVO.getTypeIdList();
        if(CrowdUtils.collectionEffectiveCheck(typeIdList)) {
            typePOMapper.insertRelationshipBatch(projectId, typeIdList);
        }

        // 4.保存tagIdList
        List<Integer> tagIdList = projectVO.getTagIdList();
        if(CrowdUtils.collectionEffectiveCheck(tagIdList)) {
            tagPOMapper.insertRelationshipBatch(projectId, tagIdList);
        }
        // 5.保存detailPicturePathList
        // ①从VO对象中获取detailPicturePathList
        List<String> detailPicturePathList = projectVO.getDetailPicturePathList();
        if (CrowdUtils.collectionEffectiveCheck(detailPicturePathList)) {
            // ②创建一个空List集合，用来存储ProjectItemPicPO对象
            List<ProjectItemPicPO> projectItemPicPOList = new ArrayList<>();
            // ③遍历detailPicturePathList
            for (String detailPath : detailPicturePathList) {
                // ④创建projectItemPicPO对象
                ProjectItemPicPO projectItemPicPO = new ProjectItemPicPO(projectId, detailPath);
                projectItemPicPOList.add(projectItemPicPO);
            }
            // ⑤根据projectItemPicPOList执行批量保存
            projectItemPicPOMapper.insertBatch(projectItemPicPOList);
        }
        // 6.保存MemberLaunchInfoPO
        MemberLauchInfoVO memberLauchInfoVO = projectVO.getMemberLauchInfoVO();
        MemberLaunchInfoPOExample memberLaunchInfoPOExample = new MemberLaunchInfoPOExample();
        MemberLaunchInfoPOExample.Criteria criteria = memberLaunchInfoPOExample.createCriteria();
        criteria.andMemberidEqualTo(id);
        memberLaunchInfoPOMapper.deleteByExample(memberLaunchInfoPOExample);
        if (memberLauchInfoVO != null) {
            MemberLaunchInfoPO memberLaunchInfoPO = new MemberLaunchInfoPO();
            BeanUtils.copyProperties(memberLauchInfoVO, memberLaunchInfoPO);
            memberLaunchInfoPO.setMemberid(Integer.parseInt(memberId));
            memberLaunchInfoPOMapper.insert(memberLaunchInfoPO);
        }
        // 7.根据ReturnVO的List保存ReturnPO
        List<ReturnVO> returnVOList = projectVO.getReturnVOList();
        if (CrowdUtils.collectionEffectiveCheck(returnVOList)) {
            List<ReturnPO> returnPOList = new ArrayList<>();
            for (ReturnVO returnVO : returnVOList) {
                ReturnPO returnPO = new ReturnPO();
                BeanUtils.copyProperties(returnVO, returnPO);
                returnPO.setProjectid(projectId);
                returnPOList.add(returnPO);
            }
            returnPOMapper.insertBatch(returnPOList);

        }

        // 8.保存MemberConfirmInfoPO
        MemberConfirmInfoVO memberConfirmInfoVO = projectVO.getMemberConfirmInfoVO();

        if(memberConfirmInfoVO != null) {
            MemberConfirmInfoPO memberConfirmInfoPO = new MemberConfirmInfoPO(Integer.parseInt(memberId), memberConfirmInfoVO.getPaynum(), memberConfirmInfoVO.getCardnum());
            memberConfirmInfoPOMapper.insert(memberConfirmInfoPO);
        }
    }

    @Override
    public List<TypePO> selecetAllType() {
        List<TypePO> typePOS = typePOMapper.selectByExample(null);
        return typePOS;
    }

    @Override
    public List<TagPO> selecetAllTag() {
        List<TagPO> tagPOS = tagPOMapper.selectByExample(null);
        return tagPOS;
    }
}
